class ExportBookButton < MenuButton

	def initialize
		super(Gtk::Stock::CONVERT)
		self.tip = _('Export this book in a different format (Ctrl+E)')
		self.signal_connect('clicked') { ExportBookButton.export_book }
	end

	def ExportBookButton.export_book
		$main.save_book

		a = Gtk::Assistant.new
		a.title = _('Export Book Wizard')
		a.set_size_request(500, 400)

		# Page 1
		page1 = Gtk::Alignment.new(0.5, 0.5, 0, 0)
		vbox = Gtk::VBox.new(false, 6)
		text = Gtk::RadioButton.new(_('Text file (*.txt)'))
		filetypes = Gtk::Frame.new(_('Output file type'))
		vbox.pack_start(text)
		vbox.border_width = 12
		filetypes << vbox
		page1 << filetypes
		page1.show_all
		a.append_page(page1)
		a.set_page_type(page1, Gtk::Assistant::PAGE_INTRO)
		a.set_page_title(page1, _('Choose the file type'))
		a.set_page_complete(page1, true)
		
		# Page 2
		page2 = Gtk::Alignment.new(0.5, 0.5, 0, 0)

		table = Gtk::Table.new(2, 2)
		table.row_spacings = table.column_spacings = 6
		txt_type = Gtk::ComboBox.new(true)
		txt_type.append_text(_('one single file'))
		txt_type.append_text(_('one file for each chapter'))
		txt_type.active = 0
		txt_line_ending = Gtk::ComboBox.new(true)
		txt_line_ending.append_text(_('Microsoft (13 10)'))
		txt_line_ending.append_text(_('Unix (13)'))
		txt_line_ending.append_text(_('Apple (10)'))
		txt_line_ending.active = 1
		txt_encoding = Gtk::ComboBox.new(true)
		txt_encoding.append_text(_('Western (iso-8859-1)'))
		txt_encoding.append_text(_('International (utf-8)'))
		txt_encoding.active = 0

		table.attach(Gtk::Label.new(_('Save book as')).set_xalign(0), 0, 1, 0, 1)
		table.attach(txt_type, 1, 2, 0, 1)
		table.attach(Gtk::Label.new(_('Line ending')).set_xalign(0), 0, 1, 1, 2)
		table.attach(txt_line_ending, 1, 2, 1, 2)
		table.attach(Gtk::Label.new(_('Codification')).set_xalign(0), 0, 1, 2, 3)
		table.attach(txt_encoding, 1, 2, 2, 3)

		page2 << table
		page2.show_all
		a.append_page(page2)
		a.set_page_title(page2, _('Choose text options'))
		a.set_page_complete(page2, true)

		# Page 3
		page3 = Gtk::VBox.new(false, 6)
		hbox = Gtk::HBox.new(false, 6)
		hbox.pack_start(Gtk::Label.new(_('Output folder:')), false, false)
		hbox.pack_start(folder = Gtk::FileChooserButton.new('', Gtk::FileChooser::ACTION_SELECT_FOLDER), true, true)

		files = Gtk::TextView.new

		page3.border_width = 12
		page3.pack_start(hbox, false, false)
		page3.pack_start(Gtk::HSeparator.new, false, false)
		page3.pack_start(Gtk::Label.new(_('The following files will be generated:')).set_xalign(0), false, false)
		page3.pack_start(Gtk::ScrolledWindow.new.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC).set_shadow_type(Gtk::SHADOW_IN).add(files))

		a.append_page(page3)
		a.set_page_title(page3, _('Choose destination folder'))
		a.set_page_complete(page3, true)
		a.set_page_type(page3, Gtk::Assistant::PAGE_CONFIRM)

		a.signal_connect('cancel') { a.destroy }
		a.signal_connect('close') do
			if text.active?
				types = [ :one_file, :many_files ]
				line_endings = [ :windows, :unix, :mac ]
				encodings = [ :latin1, :utf8 ]
				Export.export_txt($main.book, folder.filename, types[txt_type.active], line_endings[txt_line_ending.active], encodings[txt_encoding.active])
				a.destroy
			end
		end
		a.signal_connect('prepare') do |w, page|
			if page == page1
				text.set_label(_('Text file (*.txt) - iconv required')).set_sensitive(false) if not $has_iconv
			elsif page == page3
				if txt_type.active == 0
					files.buffer.text = $main.book.title.sub(' ', '_').downcase + '.txt'
				else
					s = ''
					$main.book.chapters.each do |chapter| 
						s += chapter.name.sub(' ', '_').downcase + ".txt\n"
					end
					files.buffer.text = s
				end
			end
		end

		a.show_all
	end

	def ExportBookButton.filename_format(f)
		return f.sub(' ', '_')
	end

end

